package com.ljkj.cordial.chat.ui.views;

import com.lanjiu.lib.imclient.bus.EventConstants;
import com.lanjiu.lib.imclient.bus.MessageEvent;
import com.lanjiu.lib.imclient.dto.GroupDto;
import com.lanjiu.lib.imclient.dto.GroupMemberDto;
import com.lanjiu.lib.imclient.dto.GroupVo;
import com.lanjiu.lib.imclient.dto.base.Dto;
import com.lanjiu.lib.imclient.dto.chat.ChatMessageDto;
import com.lanjiu.lib.imclient.dto.chat.TextDto;
import com.lanjiu.lib.imclient.dto.extra.ChatGroupMsgDto;
import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.lanjiu.lib.imclient.ims.IMSUtils;
import com.lanjiu.lib.imclient.user.UserPreferences;
import com.lanjiu.lib.utils.GUID;
import com.lanjiu.lib.utils.log.Logger;
import com.ljkj.cordial.chat.ui.IView;

/**
 * @description:
 * @author: Panwei
 * @date: 2019/12/27 17:08
 */
public class GroupChatSetView extends IView {

    private GroupChatSetCallback mCallback;

    public GroupChatSetView(GroupChatSetCallback mCallback) {
        this.mCallback = mCallback;
    }

    public void doGetGroupMemberList(String groupId) {
        int action = UserPreferences.isRegistered() ? EventConstants.Action.GROUP_INFO_REGISTERED : EventConstants.Action.GROUP_INFO_TOURISTS;
        doSendRequest(action, groupId + ";" + IMSContacts.Contact.OLD_GROUP);
    }

    public void doExitGroup(String groupId, String groupName, String memberId) {
        ChatGroupMsgDto chatGroupMsgDto = new ChatGroupMsgDto();
        GroupMemberDto groupMemberDto = new GroupMemberDto(IMSContacts.UserType.REGISTERED_USER);
        groupMemberDto.setGroupMemberId(memberId);
        groupMemberDto.setGroupMemberUserId(UserPreferences.getUserId());
        groupMemberDto.setGroupNemberName(UserPreferences.getNickname());
        groupMemberDto.setGroupMemberAvatar(UserPreferences.getAvatar());
        GroupDto groupDto = new GroupDto(IMSContacts.UserType.REGISTERED_USER);
        groupDto.setGroupId(groupId);
        groupDto.setGroupName(groupName);
        groupDto.setGroupType(IMSUtils.userTypeAnalysis(groupId));
        groupDto.setSnapchatStatus(false);
        groupMemberDto.setGroup(groupDto);
        chatGroupMsgDto.setGroupMemberDto(groupMemberDto);
        ChatMessageDto chatMessageDto = new ChatMessageDto();
        chatMessageDto.setChatType(IMSContacts.MsgContentType.EXIT_GROUP);
        chatMessageDto.setMsgId(GUID.random());
        TextDto textDto = new TextDto();
        textDto.setContent(UserPreferences.getNickname() + "退出了群聊");
        textDto.setLength(String.valueOf(textDto.getContent().length()));
        chatMessageDto.setText(textDto);
        chatGroupMsgDto.setChatMessageDto(chatMessageDto);
        doSendRequest(EventConstants.Action.GROUP_MEMBER_EXIT, chatGroupMsgDto.toJson());
    }

    public void doDissolveGroup(String groupId, String groupName, String memberId) {
        ChatGroupMsgDto chatGroupMsgDto = new ChatGroupMsgDto();
        GroupMemberDto groupMemberDto = new GroupMemberDto(IMSContacts.UserType.REGISTERED_USER);
        groupMemberDto.setGroupMemberId(memberId);
        groupMemberDto.setGroupMemberUserId(UserPreferences.getUserId());
        groupMemberDto.setGroupNemberName(UserPreferences.getNickname());
        groupMemberDto.setGroupMemberAvatar(UserPreferences.getAvatar());
        GroupDto groupDto = new GroupDto(IMSContacts.UserType.REGISTERED_USER);
        groupDto.setGroupId(groupId);
        groupDto.setGroupName(groupName);
        groupDto.setGroupType(IMSUtils.userTypeAnalysis(groupId));
        groupDto.setSnapchatStatus(false);
        groupMemberDto.setGroup(groupDto);
        chatGroupMsgDto.setGroupMemberDto(groupMemberDto);
        ChatMessageDto chatMessageDto = new ChatMessageDto();
        chatMessageDto.setChatType(IMSContacts.MsgContentType.DISSOLVE_GROUP);
        chatMessageDto.setMsgId(GUID.random());
        TextDto textDto = new TextDto();
        textDto.setContent(UserPreferences.getNickname() + "解散了群组");
        textDto.setLength(String.valueOf(textDto.getContent().length()));
        chatMessageDto.setText(textDto);
        chatGroupMsgDto.setChatMessageDto(chatMessageDto);
        doSendRequest(EventConstants.Action.GROUP_MEMBER_EXIT, chatGroupMsgDto.toJson());
    }

    @Override
    protected void onMessageEventHandle(MessageEvent event) {
        Logger.print("GroupChatSetView", event.toString());
        if (event.action() == EventConstants.Action.GROUP_INFO_TOURISTS
                || event.action() == EventConstants.Action.GROUP_INFO_REGISTERED) {
            GroupVo group = null;
            if (checkIsSuccess(event.code(), event.data()))
                group = Dto.fromJson(event.data(), GroupVo.class);
            if (null != mCallback)
                mCallback.onGetGroupMemberListCallback(event.code(), event.msg(), group);
        } else if (event.action() == EventConstants.Action.GROUP_MEMBER_EXIT) {
            if (null != mCallback)
                mCallback.onExitToGroupCallback(event.code(), event.msg());
        }
    }

    public interface GroupChatSetCallback {

        void onGetGroupMemberListCallback(int code, String msg, GroupVo data);

        void onExitToGroupCallback(int code, String msg);
    }
}
